import apiMixin from '@/mixin/apiMixin';
import taskMixin from '@/mixin/taskMixin';

export default {
    mixins: [apiMixin,taskMixin],
    data() {
        return {
            tableData: [],
            tableData1: [],
            loading: false,
        }
    },
    mounted() {
        this.init();
    },
    methods: {
        init() {
            this.getPushSetListApi();
        },
        getPushSetListApi() {
            let _self = this;
            this.comResquest(this.request.getPushSetList,(res)=>{
                console.log(res);
                let { result_target,result_oprations } = res;
                _self.tableData = result_target;
                _self.tableData1 = result_oprations;
            },{
                load: true, 
            })
        },
        addTextType() {
            this.tableData.push({
                "regex": "",
                "operations": "",
                "name": "",
                "uid":new Date().getTime(),
            });
        },
        deleteTextType(delIndex) {
            if(this.tableData.length < 2) {
                this.$message.warning('都最后一个了，不能删啦！');
                return;
            }
            let _self = this;
            this.deletePushSetByIdApi({
                id: this.tableData[delIndex].id,
                type: 'operation_target'
            },()=>{
                _self.tableData.splice(delIndex,1);
            });
        },
        addWayType() {
            this.tableData1.push({
                "h5_command": "",
                "app_command": "",
                "h5_name": "",
                "app_name": "",
                "h5_ext": "",
                "app_ext": "",
                "uid":new Date().getTime()
            });
        },
        deleteWayType(delIndex) {
            if(this.tableData1.length < 2) {
                this.$message.warning('都最后一个了，不能删啦！');
                return;
            }
            let _self = this;
            this.deletePushSetByIdApi({
                id: this.tableData1[delIndex].id,
                type: 'operation'
            },()=>{
                _self.tableData1.splice(delIndex,1);
            });
        },
        deletePushSetByIdApi(resPar,cb) {
            let _self = this;
            this.comResquest(this.request.deletePushSetById,(res)=>{
                _self.init();
                console.log(res);
                _self.$message.success('删除成功');
                cb && cb();
            },{
                id: resPar.id,
                type: resPar.type,
                load: true, 
            })
        },
        sumbit() {
            let _self = this;
            for (let textIndex=0;textIndex < this.tableData.length; textIndex++) {
                let curText = this.tableData[textIndex];
                if (!curText.name) {
                    this.$message.warning('文本类型中有未填写完的表单');
                    return;
                }
            }
            for (let wayIndex=0;wayIndex < this.tableData1.length; wayIndex++) {
                let curWay = this.tableData1[wayIndex];
                if (!curWay.app_name && !curWay.h5_name) {
                    this.$message.warning('方法列表中有未填写完的表单');
                    return;
                }
            }
            this.comResquest(this.request.postEditPushSet,(res)=>{
                console.log(res);
                _self.init();
                _self.$message.success('保存成功');
            },{
                result_target: this.tableData,
                result_oprations: this.tableData1,
                load: true, 
            })
        }
    },
}